package cn.monkey.family.server.domain.uc.auth.rbac;

import cn.monkey.family.rbac.dsl.http.HttpRequestPredicateFactory;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RBACConfig {

    @Bean
    @ConfigurationProperties("auth.rbac.http.ignored")
    IgnoredHttpRequestProperties ignoredHttpRequestProperties() {
        return new IgnoredHttpRequestProperties();
    }

    @Bean
    FilterRegistrationBean<RBACFilter> authenticateFilterRegistrationBean(HttpRequestPredicateFactory<HttpServletRequest> httpRequestPredicateFactory,
                                                                          IgnoredHttpRequestProperties ignoredHttpRequestProperties) {
        FilterRegistrationBean<RBACFilter> filterRegistrationBean = new FilterRegistrationBean<>();
        filterRegistrationBean.setFilter(new RBACFilter(httpRequestPredicateFactory, ignoredHttpRequestProperties.getDefinitions()));
        filterRegistrationBean.setOrder(2);
        return filterRegistrationBean;
    }
}
